package simpledb.execution;

import simpledb.algorithm.Join.JoinStrategy;
import simpledb.algorithm.Join.NestedLoopJoin;
import simpledb.algorithm.Join.SortMergeJoin;
import simpledb.storage.TupleIterator;
import simpledb.transaction.TransactionAbortedException;
import simpledb.common.DbException;
import simpledb.storage.Tuple;
import simpledb.storage.TupleDesc;

import java.util.*;

/**
 * The Join operator implements the relational join operation.
 */
public class Join extends Operator {

    private static final long serialVersionUID = 1L;

    private JoinPredicate     joinPredicate;
    private OpIterator        child1;
    private OpIterator        child2;
    private TupleDesc         td;

    private JoinStrategy      joinStrategy;
    private TupleIterator     iterator;

    /**
     * Constructor. Accepts two children to join and the predicate to join them
     * on
     * 
     * @param p
     *            The predicate to use to join the children
     * @param child1
     *            Iterator for the left(outer) relation to join
     * @param child2
     *            Iterator for the right(inner) relation to join
     */
    public Join(JoinPredicate p, OpIterator child1, OpIterator child2) {
        // some code goes here
        this.joinPredicate = p;
        this.child1 = child1;
        this.child2 = child2;
        final TupleDesc td1 = child1.getTupleDesc();
        final TupleDesc td2 = child2.getTupleDesc();
        final ArrayList<TupleDesc.TDItem> tdItems = new ArrayList<>();
        tdItems.addAll(td1.getDescList());
        tdItems.addAll(td2.getDescList());
        this.td = new TupleDesc(tdItems);
    }

    public JoinPredicate getJoinPredicate() {
        // some code goes here
        return this.joinPredicate;
    }

    /**
     * @return
     *       the field name of join field1. Should be quantified by
     *       alias or table name.
     * */
    public String getJoinField1Name() {
        // some code goes here
        final int field1 = this.joinPredicate.getField1();
        return this.child1.getTupleDesc().getFieldName(field1);
    }

    /**
     * @return
     *       the field name of join field2. Should be quantified by
     *       alias or table name.
     * */
    public String getJoinField2Name() {
        // some code goes here
        final int field2 = this.joinPredicate.getField2();
        return this.child2.getTupleDesc().getFieldName(field2);
    }

    /**
     * @see TupleDesc#merge(TupleDesc, TupleDesc) for possible
     *      implementation logic.
     */
    public TupleDesc getTupleDesc() {
        // some code goes here
        return this.td;
    }

    public void open() throws DbException, NoSuchElementException, TransactionAbortedException {
        // some code goes here
        this.child1.open();
        this.child2.open();
        super.open();
        // You can choose sortMerge join, hash join, or nested loop join
        this.joinStrategy = new NestedLoopJoin(child1, child2, this.td, this.joinPredicate);
        this.iterator = this.joinStrategy.doJoin();
        this.iterator.open();
    }

    public void close() {
        // some code goes here
        this.joinStrategy.close();
        this.iterator.close();
        this.child1.close();
        this.child2.close();
        super.close();
    }

    public void rewind() throws DbException, TransactionAbortedException {
        // some code goes here
        close();
        open();
    }

    /**
     * Returns the next tuple generated by the join, or null if there are no
     * more tuples. Logically, this is the next tuple in r1 cross r2 that
     * satisfies the join predicate. There are many possible implementations;
     * the simplest is a nested loops join.
     * <p>
     * Note that the tuples returned from this particular implementation of Join
     * are simply the concatenation of joining tuples from the left and right
     * relation. Therefore, if an equality predicate is used there will be two
     * copies of the join attribute in the results. (Removing such duplicate
     * columns can be done with an additional projection operator if needed.)
     * <p>
     * For example, if one tuple is {1,2,3} and the other tuple is {1,5,6},
     * joined on equality of the first column, then this returns {1,2,3,1,5,6}.
     * 
     * @return The next matching tuple.
     * @see JoinPredicate#filter
     */
    protected Tuple fetchNext() throws TransactionAbortedException, DbException {
        // some code goes here
        if (this.iterator.hasNext()) {
            return this.iterator.next();
        }
        return null;
    }

    @Override
    public OpIterator[] getChildren() {
        // some code goes here
        return new OpIterator[] { this.child1, this.child2 };
    }

    @Override
    public void setChildren(OpIterator[] children) {
        // some code goes here
        if (children.length == 2) {
            this.child1 = children[0];
            this.child2 = children[1];
        }
    }

}
